// Copyright (C) 2017 Mozilla Corporation. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-getiterator
description: >
    `GetIterator(obj, ~async~)` must attempt to call `obj[@@asyncIterator]` when
    that value is an object with an [[IsHTMLDDA]] internal slot, not act as if
    the value were `undefined`.
features: [async-iteration, IsHTMLDDA]
flags: [async]
---*/

function Test() {
    async function f() {
      var IsHTMLDDA = $262.IsHTMLDDA;
      var iter = {
        [Symbol.asyncIterator]: IsHTMLDDA,
        get [Symbol.iterator]() {
          throw new Test262Error("shouldn't touch Symbol.iterator");
        },
      };

      // `IsHTMLDDA` is called here with `iter` as `this` and no arguments, and it's
      // expected to return `null` under these conditions.  Then the iteration
      // protocol throws a `TypeError` because `null` isn't an object.
      for await (var x of iter)
        return "for-await-of body shouldn't be reached";

      return "should have failed earlier";
    }

    f().then($DONE,
             function (e) {
               assert.sameValue(e.constructor, TypeError,
                                "expected TypeError because " +
                                "`iter[Symbol.asyncIterator]() returned a " +
                                "non-object: " + e);
             })
       .then($DONE, $DONE);
}

// preheat
const NUMBER_OF_PREHEAT = 1;
for (let i = 0; i < NUMBER_OF_PREHEAT; i++) {
    Test();
}

ArkTools.jitCompileAsync(Test);
assert.sameValue(ArkTools.waitJitCompileFinish(Test), true, '[JIT] JIT disabled.');

// test
const NUMBER_OF_TEST = 1; // or 2
for (let i = 0; i < NUMBER_OF_TEST; i++) {
    Test();
}
